var PromptComponent = easyMe.import("public/javascripts/widget/prompt/prompt.js");
var DialogComponent = easyMe.import("public/javascripts/widget/dialog/dialog.js");
var picPreview = easyMe.import("public/javascripts/widget/picPreview/picPreview.js");

var AnnexComponent = React.createClass({
    componentDidMount: function() {
        easyMe.useJs(['common/uploadFile/uploadFile.js']);
        if(this.state.info.oid){
            this.getUploadFiles(false,this.state.info.oid)
        }else if(this.state.info.from === "highVolumeOrders"){
            this.getHighVolumeUploadFiles(); //批量下单的接口和其它的不同

        }else{
            this.getUploadFiles(true,'')
        }
        picPreview.init();
    },
    receiveMsg: function(data) {
        console.log(data);
    },
    getInitialState: function () {
        return this.props ? this.props : {};
    },
    getHighVolumeUploadFiles:function(){
        var id = easyMe.getQueryString('highVolumeOrderId') ;
        if(!id){
            console.log("id error");
            return;
        }

        var orderStatus= 'DRAFT';
        var param = {
            system:'customer',
            edit:true,
            btnHandle:true,
            orderStatus:orderStatus,
            append:'#annexContent',
            orderFileType:{
                url:'/api/v1/order/attaches/fileTypes',
                type:'GET'
            },
            getOrderFile:{
                url:'/api/v1/order/batches/'+id+'/attatches',
                type:'GET',
                param:{
                    pageIndex:1,
                    pageSize:50,
                    orderNo:id
                }
            },
            prompt:PromptComponent,
            dialog:DialogComponent
        };


        this.setState({"fileDtos":$(document).uploadBox(param)});


    },
    getUploadFiles:function(flag,id){
        var orderStatus= 'DRAFT';
        var param = {
            system:'customer',
            edit:flag,
            btnHandle:true,
            orderStatus:orderStatus,
            append:'#annexContent',
            orderNo:id,
            orderFileType:{
                url:'/api/v1/order/attaches/fileTypes',
                type:'GET'
            },
            getOrderFile:{
                url:'/api/v1/order/attaches',
                type:'GET',
                param:{
                    pageIndex:1,
                    pageSize:50,
                    orderNo:id
                }
            },
            delOrderFile:{
                url:'',
                type:'POST',
                param:{
                    orderNo:id,
                    orderFileId:''
                }
            },
            addOrderFile:{
                url:'/api/v1/order/attaches/'+id+'/orderFiles',
                type:'POST',
                param:{
                    orderNo:id,
                    orderFileId:''
                }
            },
            ckeckFileName:{
                url:'/api/v1/order/attaches/name/check',
                type:'GET',
                param:{
                    orderNo:id,
                    orderFileId:''
                }
            },
            prompt:PromptComponent,
            dialog:DialogComponent
        };

        this.setState({"fileDtos":$(document).uploadBox(param)});
    },
    getData:function (callback) {
        var oid = this.state.info.oid ? this.state.info.oid : '';
        if(oid){
            this.state.fileDtos.forEach(function(item, index){
                item.orderNo = oid;
            });
        }
        if(this.state.info.from === "highVolumeOrders"){
            //前端判断重复
            var nameArray =[];
            this.state.fileDtos.forEach(function (val) {
                nameArray.push(val.fileName)
            });
            nameArray.sort();
            for(var i=0;i<nameArray.length;i++){
                if(nameArray[i] === nameArray[i+1]){
                    //有重复
                    PromptComponent.init({'type': 'warning', "msg":"附件的名称有重复"}).show();
                    return
                }
            }
        }

        callback(this.state.fileDtos);

    },
    render:function() {

        var data = this.state.data;
        var info = this.state.info;
        return ~annex;
    }
});
module.exports = AnnexComponent;
